package com.lottery.game.runner;

import com.lottery.game.core.modules.service.app.OperationalSetService;
import com.lottery.game.core.modules.service.redis.RedisService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;

import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

/**
 * DummyCheckRunner  替换VirtualNumberSetRunner
 * 扫描符合在线的虚拟人数
 * @author Administrator
 *
 */
@Deprecated
//@Component
public class VirtualNumberSetRunner implements ApplicationRunner {
	@Autowired
	private RedisService redisFeignClient;

    @Autowired
	private OperationalSetService operationalSetService;

	Logger logger = LoggerFactory.getLogger(VirtualNumberSetRunner.class);


	@Override
	public void run(ApplicationArguments args) throws Exception {
		logger.info("开始更新虚拟人数。。。。");
		 TimerTask task = new TimerTask() {
	            @Override
	            public void run() {
	            	//Jedis jedis =null;
	            	try {
	      			  List<Map<String, Object>> list=operationalSetService.queryVirtualNumberSet();
	      	            		if(list!=null&&list.size()>=1) {
	      	            			//循环数据
	      	            			for (int i = 0; i < list.size(); i++) {
	      	            				Map<String, Object> itemMap =list.get(i);
	      	            				String  roomNumber=(String)itemMap.get("roomNumber");//获取房间号
	      	            				int max = Integer.parseInt((String)itemMap.get("virtualNumberMax"));
	      	            				int min =  Integer.parseInt((String)itemMap.get("virtualNumber"));;
	      	            				Random random = new Random();
	      	            				int randoms = random.nextInt(max - min + 1) + min;
	      	            				String randomInfo=String.valueOf(randoms);
	      	            				redisFeignClient.set(roomNumber, randomInfo);
	      							}
	      	            		}

	        		} catch (Exception e) {
	        			logger.error("VirtualNumberSetRunner  error : " + e);
	        		}


	            }
	        };

	        Timer timer = new Timer();
	        timer.scheduleAtFixedRate(task, 0, 600000);
	}

}
